package br.com.fafica.sisape.view;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Color;
import javax.swing.JLabel;
import java.awt.Font;
import java.util.ArrayList;

import javax.swing.SwingConstants;
import javax.swing.JComboBox;
import javax.swing.JButton;

import br.com.fafica.sisape.control.ControllerClinica;
import br.com.fafica.sisape.control.ControllerHospital;
import br.com.fafica.sisape.control.ControllerUsuario;
import br.com.fafica.sisape.model.Clinica;
import br.com.fafica.sisape.model.Hospital;
import br.com.fafica.sisape.model.Paciente;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class TelaHospitalEscolherUsuario extends JFrame {

	private JPanel contentPane;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TelaClinicaEscolherUsuario frame = new TelaClinicaEscolherUsuario();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public TelaHospitalEscolherUsuario() {
		setResizable(false);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 202);
		contentPane = new JPanel();
		contentPane.setBackground(Color.WHITE);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblClinica = new JLabel("Hospital:");
		lblClinica.setHorizontalAlignment(SwingConstants.RIGHT);
		lblClinica.setForeground(new Color(0, 0, 128));
		lblClinica.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblClinica.setBounds(60, 46, 57, 19);
		contentPane.add(lblClinica);
		
		final JComboBox comboHospital = new JComboBox();
		comboHospital.setForeground(new Color(0, 0, 128));
		comboHospital.setFont(new Font("Tahoma", Font.PLAIN, 12));
		comboHospital.setBounds(127, 38, 235, 27);
		contentPane.add(comboHospital);
		
		ControllerHospital controlHospi = new ControllerHospital();
		ArrayList<Hospital> listaHospital = new ArrayList<Hospital>();
		
		listaHospital = controlHospi.listarHospitais();
		
		comboHospital.addItem("");
		if(listaHospital.size() > 0){
			Hospital h;
			for(int i=0; i < listaHospital.size(); i++){
				h = listaHospital.get(i);
				comboHospital.addItem(h.getNome());
			}
		}
		
		JButton btnCadastrese = new JButton("Cadastre-se");
		btnCadastrese.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				TelaHospitalCadastra telaCadastra = new TelaHospitalCadastra();
				telaCadastra.setLocation(0, 250);
				telaCadastra.setVisible(true);
				
			}
		});
		btnCadastrese.setFont(new Font("Tahoma", Font.PLAIN, 12));
		btnCadastrese.setForeground(new Color(0, 0, 128));
		btnCadastrese.setBounds(60, 106, 114, 38);
		contentPane.add(btnCadastrese);
		
		JButton btnEntrar = new JButton("Entrar");
		btnEntrar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String nome = (String) comboHospital.getSelectedItem();
				Hospital h;
				ControllerHospital control = new ControllerHospital();
				ArrayList listaHospitais = control.listarHospitais();
				for(int i=0; i < listaHospitais.size(); i++){
					h = (Hospital) listaHospitais.get(i);
					if(h.getNome().equals(nome)){
						ControllerHospital.hospitalLogado = h;
						TelaHospitalPrincipal tela = new TelaHospitalPrincipal();
						tela.setLocation(0, 250);
						tela.setVisible(true);
						dispose();
					}
				}
				
			}
		});
		btnEntrar.setForeground(new Color(0, 0, 128));
		btnEntrar.setFont(new Font("Tahoma", Font.PLAIN, 12));
		btnEntrar.setBounds(279, 106, 114, 38);
		contentPane.add(btnEntrar);
	}
}